﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Objects;
using ArtWorldObjects;
using WorldOfArtWebApp.DataModels;

namespace WorldOfArtWebApp.DataAccess
{
    public class ArtWorldCollectionArtist : IArtWorldCollection<Artist>
    {
        protected WorldOfArtContext m_context;
        protected List<Artist> m_artistList;
        protected string m_exceptionMessage;
        protected Artist m_artist;

        public ArtWorldCollectionArtist()
        {
            m_exceptionMessage = string.Empty;
            m_context = new WorldOfArtContext();
            m_artistList = new List<Artist>();
            GetArtCollectionData();
        }

        public ObjectContext WorksOfArtContext
        {
            get { return m_context; }
        }

        public List<Artist> ArtWorldCollection
        {
            get { return m_artistList; }
        }

        public string ExceptionMessage
        {
            get { return m_exceptionMessage; }
        }

        public void GetArtCollectionData()
        {
            try
            {
                var artistQuery = from a in m_context.aspnet_Artist
                                  select a;

                foreach (var artist in artistQuery)
                {
                    m_artist = new Artist();
                    m_artist.Name = artist.FirstName + " " + artist.LastName;
                    m_artist.PlaceOfBirth = artist.City + " " + artist.Country;
                    m_artist.DateOfBirth = (DateTime)artist.BirthDay;

                    m_artistList.Add(m_artist);
                }
            }
            catch (Exception ex)
            {
                m_exceptionMessage = ex.Message;
            }
        }

        public Artist[] GetListOfArtist()
        {
            Artist[] artistList = ArtWorldCollection.ToArray();
            return artistList;
        }
    }
}